/******************************************************************************
 * @File name   :      ProcessCsvFileUtil.java
 *
 * @Author      :      GUDONG
 *
 * @Date        :      2015年10月14日
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2015年10月14日 下午1:44:06        GUDONG     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.dealerinfosync.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.cap.bts.crmdp.dealer.masterdata.dto.MarsDataConversionMemberDto;
import com.cap.bts.crmdp.dealer.masterdata.dto.MarsDataConversionPortfolioDto;
import com.cap.bts.crmdp.dealer.masterdata.dto.MasterDataDealerInfoDto;
import com.cap.bts.crmdp.dealer.remind.dto.CustomerDto;
import com.cap.bts.crmdp.dealer.remind.dto.ServiceRemindDto;
import com.cap.bts.crmdp.dealer.remind.dto.VehicleDto;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
public class ProcessCsvFileUtils {

    private static final Logger LOGGER = Logger.getLogger(ProcessCsvFileUtils.class);
    
    private static int k = 1;
    /**
     * @Date : Nov 10, 2015
     */
    private ProcessCsvFileUtils() {
        super();
    }
    
    
    /**
     * 文件压缩
     * @param zipFileName
     * @param inputFile
     */
    public static void zip(String zipFileName, File inputFile){
    	try {
			System.out.println("压缩中...");  
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(  
			        zipFileName));  
			BufferedOutputStream bo = new BufferedOutputStream(out);  
			zip(out, inputFile, inputFile.getName(), bo);  
			bo.close();  
			out.close(); // 输出流关闭  
			System.out.println("压缩完成");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    private static void zip(ZipOutputStream out, File f, String base,  
            BufferedOutputStream bo) throws Exception { // 方法重载  
        if (f.isDirectory()) {  
            File[] fl = f.listFiles();  
            if (fl.length == 0) {  
                out.putNextEntry(new ZipEntry(base + "/")); // 创建zip压缩进入点base  
                System.out.println(base + "/");  
            }  
            for (int i = 0; i < fl.length; i++) {  
                zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 递归遍历子文件夹  
            }  
            System.out.println("第" + k + "次递归");  
            k++;  
        } else {  
            out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入点base  
            System.out.println(base);  
            FileInputStream in = new FileInputStream(f);  
            BufferedInputStream bi = new BufferedInputStream(in);  
            int b;  
            while ((b = bi.read()) != -1) {  
                bo.write(b); // 将字节流写入当前zip目录  
            }
            bo.flush();
            bi.close();  
            in.close(); // 输入流关闭  
        }  
    }  
    
    
    /**
     * 文件解压缩
     * @param filePath 压缩包路径
     * @param outPutFilePath  解压缩目录
     */
    public static void unZip(String filePath,String outPutFilePath){
    	 
        long startTime = System.currentTimeMillis();  
        try {  
            ZipInputStream Zin = new ZipInputStream(new FileInputStream(filePath));//输入源zip路径  
            BufferedInputStream Bin = new BufferedInputStream(Zin);  
            String Parent = outPutFilePath; //输出路径（文件夹目录）  
            File Fout=null;  
            ZipEntry entry;  
            try {  
                while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){  
                    Fout=new File(Parent,entry.getName());  
                    if(!Fout.exists()){  
                        (new File(Fout.getParent())).mkdirs();  
                    }  
                    FileOutputStream out=new FileOutputStream(Fout);  
                    BufferedOutputStream Bout=new BufferedOutputStream(out);  
                    int b;  
                    while((b=Bin.read())!=-1){  
                        Bout.write(b);  
                    }
                    Bout.flush();
                    Bout.close();  
                    out.close();  
                    System.out.println(Fout+"解压成功");      
                }  
                Bin.close();  
                Zin.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        long endTime=System.currentTimeMillis();  
        System.out.println("耗费时间： "+(endTime-startTime)+" ms");  
    }
    
    /**
     * 解析CSV文件
     * 
     * @param file
     *            csv文件(路径+文件)
     * @return
     */
    public static List<String> analyzeCsv(File file) {
        List<String> dataList = new ArrayList<String>();
        BufferedReader br = null;
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(file), "gbk");
            br = new BufferedReader(reader);
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        } catch (Exception e) {
            LOGGER.error("读取文件错误", e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                    br = null;
                } catch (IOException e) {
                    LOGGER.error("analyzeCsv", e);
                }
            }
        }

        return dataList;
    }

    public static List<String> analyzeCsv(File file,String charsetName) {
        List<String> dataList = new ArrayList<String>();
        BufferedReader br = null;
        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(file), charsetName);
            br = new BufferedReader(reader);
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        } catch (Exception e) {
            LOGGER.error("读取文件错误", e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                    br = null;
                } catch (IOException e) {
                    LOGGER.error("analyzeCsv", e);
                }
            }
        }

        return dataList;
    }
    
    /**
     * 组合CSV文件的内容
     * 
     * @param dataList
     *            CSV文件的内容
     * @return
     */

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List<Map<String, Object>> combinationList(List<String> dataList) {

        Map<String, Object> dataMap = null;

        List resultList = new ArrayList();

        for (Iterator iterator = dataList.iterator(); iterator.hasNext();) {
            String data = (String) iterator.next();
            dataMap = new HashMap<String, Object>();

            String[] dataStrings = data.split(String.valueOf((char) 177));
            dataMap.put("company", dataStrings[0]);
            dataMap.put("tenantID", dataStrings[1]);
            dataMap.put("dealerId", dataStrings[2]);
            resultList.add(dataMap);
        }
        return resultList;
    }
    
    //SFTP member数据转换解析
    public static List<MarsDataConversionMemberDto> combinationMemberList(List<String> dataList) {

    	MarsDataConversionMemberDto marsDataConversionMemberDto = null;

        List<MarsDataConversionMemberDto> resultList = new ArrayList<MarsDataConversionMemberDto>();

        Iterator iterator = dataList.iterator();
        if (iterator.hasNext()) {
        	iterator.next();
        }
        while (iterator.hasNext()) {
            String data = (String) iterator.next();
            marsDataConversionMemberDto = new MarsDataConversionMemberDto();

            String[] dataStrings = data.split(",", 11);
            marsDataConversionMemberDto.setNum(dataStrings[0]);
            marsDataConversionMemberDto.setPcode(dataStrings[1]);
            marsDataConversionMemberDto.setMemberCode(dataStrings[2]);
            marsDataConversionMemberDto.setMemberName(dataStrings[3]);
            marsDataConversionMemberDto.setMobile(dataStrings[4]);
            marsDataConversionMemberDto.setCarPlate(dataStrings[5]);
            marsDataConversionMemberDto.setStoreCode(dataStrings[6]);
            marsDataConversionMemberDto.setMarsCustomerCode(dataStrings[7]);
            marsDataConversionMemberDto.setMarsVehicleCode(dataStrings[8]);
            marsDataConversionMemberDto.setMarsCustomerName(dataStrings[9]);
            marsDataConversionMemberDto.setMarsPhoneNumber(dataStrings[10]);
            resultList.add(marsDataConversionMemberDto);
        }
        return resultList;
    }
    
  //SFTP Protfolio数据转换解析
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List<MarsDataConversionPortfolioDto> combinationProtfolioList(List<String> dataList) throws ParseException {

    	MarsDataConversionPortfolioDto marsDataConversionPortfolioDto = null;

        List<MarsDataConversionPortfolioDto> resultList = new ArrayList<MarsDataConversionPortfolioDto>();
        
        Iterator iterator = dataList.iterator();
        if (iterator.hasNext()) {
        	iterator.next();
        }
        
        while (iterator.hasNext()) {
            String data = (String) iterator.next();
            marsDataConversionPortfolioDto = new MarsDataConversionPortfolioDto();

            String[] dataStrings = data.split(",", 24);
            marsDataConversionPortfolioDto.setNum(dataStrings[0]);
            marsDataConversionPortfolioDto.setPcode(dataStrings[1]);
            marsDataConversionPortfolioDto.setMemberCode(dataStrings[2]);
            marsDataConversionPortfolioDto.setMemberName(dataStrings[3]);
            marsDataConversionPortfolioDto.setMobile(dataStrings[4]);
            marsDataConversionPortfolioDto.setMarsCustomerCode(dataStrings[5]);
            marsDataConversionPortfolioDto.setCarPlate(dataStrings[6]);
            marsDataConversionPortfolioDto.setMarsCarNo(dataStrings[7]);
            marsDataConversionPortfolioDto.setCardNo(dataStrings[8]);
            marsDataConversionPortfolioDto.setDmsCardNo(dataStrings[9]);
            marsDataConversionPortfolioDto.setCardType(dataStrings[10]);
            marsDataConversionPortfolioDto.setDmsCardType(dataStrings[11]);
            marsDataConversionPortfolioDto.setStoreCode(dataStrings[12]);
            marsDataConversionPortfolioDto.setTitle(dataStrings[13]);
            marsDataConversionPortfolioDto.setDmsTitle(dataStrings[14]);
            if(StringUtils.isNotBlank(dataStrings[15])){
            	DateFormat creationDate =new SimpleDateFormat("yyyy-MM-dd");
            	marsDataConversionPortfolioDto.setCreationDate(creationDate.parse(dataStrings[15]));
            }
            if(StringUtils.isNotBlank(dataStrings[16])){
            	DateFormat validateDate =new SimpleDateFormat("yyyy-MM-dd");
            	marsDataConversionPortfolioDto.setValidateDate(validateDate.parse(dataStrings[16]));
            }
            if(StringUtils.isNotBlank(dataStrings[17])){
            	DateFormat dueDate =new SimpleDateFormat("yyyy-MM-dd");
            	marsDataConversionPortfolioDto.setDueDate(dueDate.parse(dataStrings[17]));
            }
            marsDataConversionPortfolioDto.setDpStatus(dataStrings[18]);
            marsDataConversionPortfolioDto.setDmsStatus(dataStrings[19]);
            marsDataConversionPortfolioDto.setCouponType(dataStrings[20]);
            marsDataConversionPortfolioDto.setFreeServiceTimes(dataStrings[21]);
            marsDataConversionPortfolioDto.setTimesOfUse(dataStrings[22]);
            marsDataConversionPortfolioDto.setTimesLeft(dataStrings[23]);
            resultList.add(marsDataConversionPortfolioDto);
        }
        return resultList;
    }
    
    //SFTP serviceRemind文件数据解析
    public static List<ServiceRemindDto> combinationServiceRemindList(List<String> dataList) {

    	ServiceRemindDto serviceRemindDto = null;

        List<ServiceRemindDto> resultList = new ArrayList<ServiceRemindDto>();

        Iterator iterator = dataList.iterator();
        if (iterator.hasNext()) {
        	iterator.next();
        }
        while (iterator.hasNext()) {
            String oldData = (String) iterator.next();
            serviceRemindDto = new ServiceRemindDto();
//            String data = oldData.replace("\"", "");
            String data = oldData.substring(1, oldData.length() - 1);
            if(StringUtils.isNotBlank(data)){
            	String[] dataStrings = data.split("\",\"",50);
            	serviceRemindDto.setCarPlate(dataStrings[0]);
            	serviceRemindDto.setCarModel(dataStrings[1]);
            	serviceRemindDto.setMaintenanceItem(dataStrings[2]);
            	serviceRemindDto.setLastPartBrand(dataStrings[3]);
            	serviceRemindDto.setLastPartName(dataStrings[4]);
            	serviceRemindDto.setLastMaintenanceStore(dataStrings[5]);
            	serviceRemindDto.setLastMaintenanceDate(dataStrings[6]);
            	serviceRemindDto.setLastMaintenanceMileage(dataStrings[7]);
            	serviceRemindDto.setLastCarcheckStore(dataStrings[8]);
            	serviceRemindDto.setLastCarcheckDate(dataStrings[9]);
            	serviceRemindDto.setLastCarcheckMileage(dataStrings[10]);
            	serviceRemindDto.setNextCalcbasis(dataStrings[11]);
            	serviceRemindDto.setMaintenanceSpecBasis(dataStrings[12]);
            	serviceRemindDto.setNextCalcDate(dataStrings[13]);
            	serviceRemindDto.setNextCalcMileage(dataStrings[14]);
            	if(dataStrings.length > 15){
            		serviceRemindDto.setUseStandardAverageKM(dataStrings[15]);
            	}
            	resultList.add(serviceRemindDto);
            }
        }
        return resultList;
    }
    
  	//SFTP customer文件数据解析
    public static List<CustomerDto> combinationCustomerList(List<String> dataList) throws AppException {

    	CustomerDto  customerDto = null;

        List<CustomerDto> resultList = new ArrayList<CustomerDto>();

        Iterator iterator = dataList.iterator();
        //跳过第一行的表头
        if (iterator.hasNext()) {
        	iterator.next();
        }
        while (iterator.hasNext()) {
            String oldData = (String) iterator.next();
            customerDto = new CustomerDto();
            //去除所有的引号
            String data = oldData.replace("\"", "");
            if(StringUtils.isNotBlank(data)){
            	String[] dataStrings = data.split(",",11);
                
            	try {
            		if(StringUtils.isNotBlank(dataStrings[0])){
            			customerDto.setServiceCenterID(Long.valueOf(dataStrings[0]));
            		}
	                customerDto.setCustomerID(dataStrings[1]);
	                if(StringUtils.isNotBlank(dataStrings[2])){
	                	customerDto.setMobileNumber(dataStrings[2]);
	                }
	                customerDto.setName(dataStrings[3]);
	                customerDto.setContactName(dataStrings[4]);
                	customerDto.setContactPhone(dataStrings[5]);
    				if(StringUtils.isNotBlank(dataStrings[6])){
    					DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    				    customerDto.setCreationDate(dateFormat.parse(dataStrings[6]));
    				}
    				if(StringUtils.isNotBlank(dataStrings[7])){
    					DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    				    customerDto.setLastModificationDate(dateFormat.parse(dataStrings[7]));
    				}
	                customerDto.setReceiveSMSorNot(dataStrings[8]);
	                customerDto.setCrmMember(dataStrings[9]);
	                customerDto.setCustomerType(dataStrings[10]);
	                resultList.add(customerDto);
            	} catch (ParseException e) {
            		LOGGER.error("Error combinationCustomerList", e);
            	}
            }
        }
        return resultList;
    }
    
    //SFTP vehicle文件数据解析
    public static List<VehicleDto> combinationVehicleList(List<String> dataList) throws AppException {

    	VehicleDto  vehicleDto = null;

        List<VehicleDto> resultList = new ArrayList<VehicleDto>();

        Iterator iterator = dataList.iterator();
        //跳过第一行的表头
        if (iterator.hasNext()) {
        	iterator.next();
        }
        
        while (iterator.hasNext()) {
            String oldData = (String) iterator.next();
            vehicleDto = new VehicleDto();
            //去除所有的引号
            String data = oldData.replace("\"", "");
            if(StringUtils.isNotBlank(data)){
            	String[] dataStrings = data.split(",",50);
            	try {
            		if(StringUtils.isNotBlank(dataStrings[0])){
            			vehicleDto.setServiceCenterID(Long.valueOf(dataStrings[0]));
	                }
	            	vehicleDto.setVin(dataStrings[1]);
	            	vehicleDto.setCustomerID(dataStrings[2]);
	            	vehicleDto.setVehicleBrandName(dataStrings[3]);
	            	vehicleDto.setVehicleModel(dataStrings[4]);
	            	vehicleDto.setVehicleVariant(dataStrings[5]);
	            	vehicleDto.setVehicleID(dataStrings[6]);
	            	if(StringUtils.isNotBlank(dataStrings[7])){
	            		vehicleDto.setYear(Long.valueOf(dataStrings[7]));
	                }
	            	vehicleDto.setSizeOfEngine(dataStrings[8]);
	            	vehicleDto.setLyID(dataStrings[9]);
	            	vehicleDto.setLicensePlate(dataStrings[10]);
					if(StringUtils.isNotBlank(dataStrings[11])){
						DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						vehicleDto.setCreationDate(dateFormat.parse(dataStrings[11]));
					}
					if(StringUtils.isNotBlank(dataStrings[12])){
						DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						vehicleDto.setLastModificationDate(dateFormat.parse(dataStrings[12]));
					}
					if(StringUtils.isNotBlank(dataStrings[13])){
						DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						vehicleDto.setRegisterDate(dateFormat.parse(dataStrings[13]));
					}
	            	vehicleDto.setCrmMember(dataStrings[14]);
	                resultList.add(vehicleDto);
            	} catch (ParseException e) {
            		throw new AppException("Error combinationVehicleList", e);
            	}
            }
        }
        return resultList;
    }
    //SFTP DealerInfo文件数据解析
    public static List<MasterDataDealerInfoDto> combinationDealerInfoList(List<String> dataList) throws AppException {
    	
    	List<MasterDataDealerInfoDto> dealerInfoList = new ArrayList<MasterDataDealerInfoDto>();
    	
    	Iterator iterator = dataList.iterator();
    	//跳过第一行的表头
    	if (iterator.hasNext()) {
    		iterator.next();
    	}
    	
    	while (iterator.hasNext()) {
    		String oldData = (String) iterator.next();
    		MasterDataDealerInfoDto dto = new MasterDataDealerInfoDto();
    		//去除所有的引号
    		String data = oldData.replace("\"", "");
    		if(StringUtils.isNotBlank(data)){
    			String[] dataStrings = data.split(",",50);
    			dto.setDealerNum(dataStrings[1]);
				dto.setDealerName(dataStrings[2]);
				dto.setAddress(dataStrings[3]);
				dto.setZipCode(dataStrings[4]);
				dto.setRegisteredAddress(dataStrings[5]);
				dto.setOwner(dataStrings[6]);
				dto.setContact(dataStrings[7]);
				dto.setContactName(dataStrings[7]);
				dto.setContactPhone(dataStrings[8]);
				dto.setTelephone(dataStrings[9]);
				dto.setProvinceName(dataStrings[10]);
				dto.setCityName(dataStrings[11]);
				dto.setCounty(dataStrings[12]);
				dto.setRegion(dataStrings[13]);
				dto.setRegionCode(dataStrings[14]);
				dto.setRegionConsultantMgr(dataStrings[15]);
				dto.setConsultant(dataStrings[16]);
				dto.setConsultantPhone(dataStrings[17]);
				dto.setConsultantMail(dataStrings[18]);
				dto.setSalesRepresentative(dataStrings[19]);
				if(org.apache.commons.lang3.StringUtils.isNotBlank(dataStrings[20])){
					try {
						dto.setWorkstations(Integer.parseInt(dataStrings[20]));
					} catch (NumberFormatException e) {
						LOGGER.error("Workstations NumberFormat ERROR");
					}
				}
				dto.setIfHq(dataStrings[21]);
				dto.setIfBranch(dataStrings[22]);
				if(org.apache.commons.lang3.StringUtils.isNoneBlank(dataStrings[23])){
					try {
						dto.setHqId(Long.parseLong(dataStrings[23]));
					} catch (NumberFormatException e) {
						LOGGER.error("HqId NumberFormat ERROR");
					}
				}
				try {
					if(org.apache.commons.lang3.StringUtils.isNoneBlank(dataStrings[24])){
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						dto.setProgramStartDate(sdf.parse(dataStrings[24]));
					}
				} catch (ParseException e) {
					throw new AppException("Error combinationDealerInfoList", e);
				}
				if(org.apache.commons.lang3.StringUtils.isNoneBlank(dataStrings[25])){
					try {
						dto.setLongitude(Double.parseDouble(dataStrings[25]));
					} catch (NumberFormatException e) {
						LOGGER.error("Longitude NumberFormat ERROR");
					}
				}
				if(org.apache.commons.lang3.StringUtils.isNoneBlank(dataStrings[26])){
					try {
						dto.setLatitude(Double.parseDouble(dataStrings[26]));
					} catch (NumberFormatException e) {
						LOGGER.error("Latitude NumberFormat ERROR");
					}
				}
				dto.setBusinessHours(dataStrings[27]);
				if(org.apache.commons.lang3.StringUtils.isNoneBlank(dataStrings[28])){
					if("门店加入流程中".equals(dataStrings[28])){
						dto.setStatus(0);
					}else if("正常".equals(dataStrings[28])){
						dto.setStatus(1);
					}else if("休眠".equals(dataStrings[28])){
						dto.setStatus(2);
					}else if("退出".equals(dataStrings[28])){
						dto.setStatus(3);
					}else if("降级".equals(dataStrings[28])){
						dto.setStatus(4);
					}
				}
				dto.setDealerStatus(dataStrings[28]);
				dto.setIfDirectPos(dataStrings[29]);
				dto.setRiskWaringLevel(dataStrings[30]);
				dto.setOwnerMobilePhone(dataStrings[31]);
				dto.setOwnerIdNumber(dataStrings[32]);
				dto.setConsultantNum(dataStrings[33]);
				dto.setExitPlan(dataStrings[34]);
				dto.setExitDealerTypeStatus(dataStrings[35]);
				dto.setFullName(dataStrings[36]);
				dto.setShortName(dataStrings[37]);
				try {
					if(org.apache.commons.lang3.StringUtils.isNoneBlank(dataStrings[38])){
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						dto.setExitDate(sdf.parse(dataStrings[38]));
					}
				} catch (ParseException e) {
					LOGGER.error("setExitDateError" + e);
				}
				dealerInfoList.add(dto);
    		}
    	}
    	return dealerInfoList;
    }

    /**
     * 将一个 Map 对象转化为一个 JavaBean
     * 
     * @param type
     *            要转化的类型
     * @param map
     *            包含属性值的 map
     * @return 转化出来的 JavaBean 对象
     * @throws IntrospectionException
     *             如果分析类属性失败
     * @throws IllegalAccessException
     *             如果实例化 JavaBean 失败
     * @throws InstantiationException
     *             如果实例化 JavaBean 失败
     * @throws InvocationTargetException
     *             如果调用属性的 setter 方法失败
     */
    @SuppressWarnings("rawtypes")
    public static Object convertMap(Class type, Map map) throws AppException {
        // 获取类属性
        BeanInfo beanInfo;
        Object obj;
        try {
            beanInfo = Introspector.getBeanInfo(type);
            // 创建 JavaBean 对象
            obj = type.newInstance();

            // 给 JavaBean 对象的属性赋值
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();

                if (map.containsKey(propertyName)) {
                    // 下面一句可以 try 起来，这样当一个属性赋值失败的时候就不会影响其他属性赋值。
                    Object value = map.get(propertyName);

                    Object[] args = new Object[1];
                    args[0] = value;

                    descriptor.getWriteMethod().invoke(obj, args);

                }

            }
        } catch (IllegalArgumentException e) {
            throw new AppException("convertMap", e);
        } catch (InvocationTargetException e) {
            throw new AppException("convertMap", e);
        } catch (InstantiationException e) {
            throw new AppException("convertMap", e);
        } catch (IntrospectionException e) {
            throw new AppException("convertMap", e);
        } catch (IllegalAccessException e) {
            throw new AppException("convertMap", e);
        }
        return obj;
    }
    
    public static void main(String[] args) {
		unZip("C://Users/gudong/Desktop/ServiceReminderWeCaretoDataHub20160106/ServiceReminder_20151229_105259.zip",
				"C://Users/gudong/Desktop/ServiceReminderWeCaretoDataHub20160106");
		zip("C://Users/gudong/Desktop/ServiceReminderWeCaretoDataHub20160106/testZip.zip"
				, new File("C:/Users/gudong/Documents/Tencent Files"));
	}
}
